﻿namespace Dcms.Config
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    public class DefaultConfigFileManager
    {
        private static string _configfilepath;
        private static IConfigInfo _configinfo = null;
        private static object _lockHelper = new object();

        public static IConfigInfo DeserializeInfo(string configfilepath, Type configtype)
        {
            IConfigInfo iconfiginfo;
            FileStream fs = null;
            try
            {
                fs = new FileStream(configfilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(configtype);
                iconfiginfo = (IConfigInfo) serializer.Deserialize(fs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null) fs.Close();
            }
            return iconfiginfo;
        }

        protected static IConfigInfo LoadConfig(ref DateTime fileoldchange, string configFilePath, IConfigInfo configinfo, bool checkTime)
        {
            object csTemp0002;
            _configfilepath = configFilePath;
            _configinfo = configinfo;
            if (checkTime)
            {
                DateTime _filenewchange = File.GetLastWriteTime(configFilePath);
                if (fileoldchange != _filenewchange)
                {
                    fileoldchange = _filenewchange;
                    lock ((csTemp0002 = _lockHelper))
                    {
                        _configinfo = DeserializeInfo(configFilePath, configinfo.GetType());
                    }
                }
            }
            else
            {
                lock ((csTemp0002 = _lockHelper))
                {
                    _configinfo = DeserializeInfo(configFilePath, configinfo.GetType());
                }
            }
            return _configinfo;
        }

        public static bool SaveConfig(string configFilePath, IConfigInfo configinfo)
        {
            bool succeed = false;
            FileStream fs = null;
            try
            {
                fs = new FileStream(configFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                new XmlSerializer(configinfo.GetType()).Serialize((Stream) fs, configinfo);
                succeed = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fs != null) fs.Close();
            }
            return succeed;
        }

        public static string ConfigFilePath
        {
            get
            {
                return _configfilepath;
            }
            set
            {
                _configfilepath = value;
            }
        }

        public static IConfigInfo ConfigInfo
        {
            get
            {
                return _configinfo;
            }
            set
            {
                _configinfo = value;
            }
        }
    }
}

